<?php

/**
 * Description of ContentNews
 *
 * @author Honza
 */
class ContentNews implements IMainBody {

  public function getContent() {
    $content = '<h2>News</h2>';

    if (isset($_GET['ida'])) {
      //show one news
      $newsID = addslashes($_GET['ida']);
      $sql = 'select id_news, title, text, DATE_FORMAT(date, \'%Y-%m-%d\') as datum from ' . TBL_NEWS . ' where id_news =' . $newsID;
      $news = DB::sql($sql);

      $title = $news[0]['title'];
      $text = $news[0]['text'];
      $date = $news[0]['datum'];


      $content .= "<h4>$title</h4>";
      $content .= "<h5>$date</h5>";
      $content .= $text;

      $content .= '<p><a href=index.php?where=news>Next news</a></p>';
    } else {
      //show table of all news
      
      $sql = 'select id_news, title, DATE_FORMAT(date, \'%Y-%m-%d\') as date from ' . TBL_NEWS;

      $result = DB::sql($sql);

      $table = '<table>';
      $table .= '<tr>';
      $table .= '<th>Title</th><th>Date</th><th>Show</th>';
      $table .= '</tr>';
      foreach ($result as &$row) {
        $newsID = $row['id_news'];

        $table .= '<tr>';
        $table .= '<td>' . $row['title'] . '</td>';
        $table .= '<td>' . $row['date'] . '</td>';
        $table .= '<td><a href="index.php?where=news&ida=' . $newsID . '">Show</a></td>';
        $table .= '</tr>';
      }
      $table .= '';
      $table .= '</table>';

      $content .= $table;
    }

    return $content;
  }

}

?>
